import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.SQLException;

import javax.swing.JComboBox;

/*
 * CompanyInfo.java : This class shows basic information about Company.
 *
 * @Author : BilCIS
 */

public class CompanyInfo extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3771544797269400488L;
	// Variables declaration
	
	private JPanel contentPane;
	private JPanel fire;
	
	private String[] years = {"---","2009", "2010", "2011", "2012", "2013"};
	private JComboBox<String> comboBox;
	
	private JLabel logo;
	
	private JTextField txtCompanyName;
	private JTextField txtCompanyAddress;
	private JTextField txtTotalValue;
	private JTextField txtProfit;
	private JTextField txtRealProfit;
	private JTextField txtBilkentCompanyInteraction;
	private JTextField txtTepePrimeFloor;
	private JTextField totalAssetTF;
	private JTextField revenueTF;
	private JTextField incomeTF;
	private JTextField txtProfit_1;
	private JTextField profitTF;
	private JTextField txtTotalEquity;
	private JTextField totalEqualityTF;
	private JTextField txtEmployees;
	private JTextField numOfEmployeesTF;
	
	private JButton btnFireEmployee;
	private	JButton btnHireEmployee;
	private	JButton btnAddIncome;

	private Employee emp;
	private Mysql mysql;
	
	private DefaultListModel<String> dlm = null;
	private JList<String> list;
	// End of variables declaration

	public CompanyInfo( Employee emp )
	{
		mysql = new Mysql (emp.getID());
		this.emp = emp;
		create();
		this.setVisible(true);
	}
	
	// Graphic Declaration
	
	public void create()
	{
		//
		//Create controls
		//
		contentPane = new JPanel();
		txtCompanyName = new JTextField("Company Name");
		txtCompanyAddress = new JTextField("Company Address");
		txtTotalValue = new JTextField("Total Assets");
		txtProfit = new JTextField("Employee Salaries");
		txtRealProfit = new JTextField("Operating Income");
		btnAddIncome = new JButton("+");
		btnHireEmployee = new JButton("Hire Employee");
		txtTepePrimeFloor = new JTextField("Tepe Prime Plaza, Floor: 32");
		txtBilkentCompanyInteraction = new JTextField("Bilkent Company Interaction Systems");
		btnFireEmployee = new JButton("Fire Employee");
		profitTF = new JTextField("40.0 $");
		totalAssetTF = new JTextField("---");
		revenueTF = new JTextField("");
		incomeTF = new JTextField("5000.0 $");
		txtProfit_1 = new JTextField("Profit");
		txtTotalEquity = new JTextField("Total Equity");
		totalEqualityTF = new JTextField("");
		txtEmployees = new JTextField("Number of Employees");
		numOfEmployeesTF = new JTextField("");
		comboBox = new JComboBox<String>(years);
		logo = new JLabel( new ImageIcon( new File("logo.gif").getAbsolutePath() ));
		//	
		//Register action listeners
		//
		comboBox.addActionListener(new targetCurrencyBox_Action());
		btnHireEmployee.addActionListener(new btnHireEmployee_Action());
		btnFireEmployee.addActionListener(new btnFireEmployee_Action());
		btnAddIncome.addActionListener(new btnAddIncome_Action());
		//	
		// Mnemonics
		//
		btnHireEmployee.setMnemonic('h');
		btnFireEmployee.setMnemonic('f');
		btnAddIncome.setMnemonic('a');
		//
		//Add controls to pane
		//
		this.add(contentPane);
		contentPane.add(txtCompanyName);
		contentPane.add(txtCompanyAddress);
		contentPane.add(txtTotalValue);
		contentPane.add(txtProfit);
		contentPane.add(txtRealProfit);
		contentPane.add(btnAddIncome);
		contentPane.add(btnFireEmployee);
		contentPane.add(btnHireEmployee);
		contentPane.add(txtBilkentCompanyInteraction);
		contentPane.add(txtTepePrimeFloor);
		contentPane.add(profitTF);
		contentPane.add(totalAssetTF);
		contentPane.add(revenueTF);
		contentPane.add(incomeTF);
		contentPane.add(txtProfit_1);
		contentPane.add(txtTotalEquity);
		contentPane.add(totalEqualityTF);
		contentPane.add(txtEmployees);
		contentPane.add(numOfEmployeesTF);
		contentPane.add(comboBox);
		contentPane.add(logo);
		//	
		//Set bounds
		//	
		contentPane.setBounds(0, 0, 450, 450);
		txtCompanyName.setBounds(10, 118, 130, 20);
		txtCompanyAddress.setBounds(10, 137, 130, 20);
		txtTotalValue.setBounds(10, 168, 130, 20);
		txtProfit.setBounds(10, 187, 130, 20);
		txtRealProfit.setBounds(10, 206, 130, 20);
		btnAddIncome.setBounds(350, 206, 40, 20);
		btnHireEmployee.setBounds(76, 318, 117, 23);
		btnFireEmployee.setBounds(242, 318, 117, 23);
		txtBilkentCompanyInteraction.setBounds(139, 118, 209, 20);
		txtTepePrimeFloor.setBounds(139, 137, 209, 20);
		profitTF.setBounds(139, 225, 209, 20);
		totalAssetTF.setBounds(139, 168, 209, 20);
		revenueTF.setBounds(139, 187, 209, 20);
		incomeTF.setBounds(139, 206, 209, 20);
		txtProfit_1.setBounds(10, 225, 130, 20);
		txtTotalEquity.setBounds(10, 244, 130, 20);
		totalEqualityTF.setBounds(139, 244, 209, 20);
		txtEmployees.setBounds(10, 263, 130, 20);
		numOfEmployeesTF.setBounds(139, 263, 209, 20);
		comboBox.setBounds(350, 168, 62, 20);
		logo.setBounds(95,-95,254,300);
		//	
		//Set editables - layouts - columns
		//	
		contentPane.setLayout(null);
		txtCompanyName.setEditable(false);
		txtCompanyName.setColumns(10);
		txtCompanyAddress.setEditable(false);
		txtCompanyAddress.setColumns(10);
		txtTotalValue.setEditable(false);
		txtTotalValue.setColumns(10);
		txtProfit.setEditable(false);
		txtProfit.setColumns(10);
		txtRealProfit.setEditable(false);
		txtRealProfit.setColumns(10);
		txtBilkentCompanyInteraction.setEditable(false);
		txtBilkentCompanyInteraction.setColumns(10);
		txtTepePrimeFloor.setEditable(false);
		txtTepePrimeFloor.setColumns(10);
		totalAssetTF.setEditable(false);
		totalAssetTF.setColumns(10);
		revenueTF.setEditable(false);
		revenueTF.setColumns(10);
		incomeTF.setEditable(false);
		incomeTF.setColumns(10);
		profitTF.setEditable(false);
		profitTF.setColumns(10);
		txtProfit_1.setEditable(false);
		txtProfit_1.setColumns(10);
		txtTotalEquity.setEditable(false);
		txtTotalEquity.setColumns(10);
		totalEqualityTF.setEditable(false);
		totalEqualityTF.setColumns(10);
		txtEmployees.setEditable(false);
		txtEmployees.setColumns(10);
		numOfEmployeesTF.setEditable(false);
		numOfEmployeesTF.setColumns(10);
		try {
			numOfEmployeesTF.setText(mysql.getNumOfEmp() + "");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			revenueTF.setText(mysql.getTotalSalary()  + " $");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		int equity = Integer.parseInt(incomeTF.getText().substring(0,incomeTF.getText().length()-4) )
					- Integer.parseInt(revenueTF.getText().substring(0,incomeTF.getText().length()-4) );
		totalEqualityTF.setText( equity + ".0 $");
		
		btnAddIncome.setBackground(Color.orange);
		btnAddIncome.setFont(new Font("Comic Sans MS", Font.BOLD, 10));
		
		//********************** Colors *********************//
		contentPane.setBackground(emp.getColor());
			
		//
		// CompanyInfo
		//	
		this.setSize(new Dimension(430, 350));
		this.setLayout(null);
	}
	
	// gets income and revenue and changes it with given values
	public class btnAddIncome_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			int salary = Integer.parseInt(JOptionPane.showInputDialog(null, "Add new income"));
			int newIncome = Integer.parseInt( incomeTF.getText().substring(0,incomeTF.getText().length()-4) ) + salary; 
			incomeTF.setText(newIncome + ".0 $");
			int equity = Integer.parseInt(incomeTF.getText().substring(0,incomeTF.getText().length()-4) )
					- Integer.parseInt(revenueTF.getText().substring(0,incomeTF.getText().length()-4) );
			totalEqualityTF.setText( equity + ".0 $");
		}
	}
	
	// opens the newform frame
	public class btnHireEmployee_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			new NewForm(emp);
		}
	}
	
	// shows the employees and can fire them (means deletes from database)
	public class btnFireEmployee_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			fire = new JPanel();
			try {
				dlm = mysql.getEmp();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

			list = new JList<String> (dlm); //data has type Object[]
			list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
			list.setSelectedIndex(2);
			
			JButton fireBtn = new JButton("Fire");
			fireBtn.addActionListener(new fireBtn_Action());

			
			fire.add(list);
			fire.add(fireBtn);
			JOptionPane.showMessageDialog(null, fire);
		}
	}
	
	// fires button	
	public class fireBtn_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			
			int i = list.getSelectedIndex();
			String em = dlm.getElementAt(i);
			em = em.substring(0,em.indexOf("|"));
			em = em.trim();
			
			try {
				mysql.deleteID(em);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			dlm.remove(i);
		}
	}

	// target currency
	public class targetCurrencyBox_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
        	String w = comboBox.getSelectedItem().toString();
        	
        	if(w == "2009")
        	{
        		totalAssetTF.setText("850.0 $");
        	}
        	else if(w == "2010")
        	{
        		totalAssetTF.setText("750.0 $");
        	}
        	else if(w == "2011")
        	{
        		totalAssetTF.setText("250.0 $");
        	}
        	else if(w == "2012")
        	{
        		totalAssetTF.setText("550.0 $");
        	}
        	else if(w == "2013")
        	{
        		totalAssetTF.setText("350.0 $");
        	}
        	else if(w == "---")
        	{
        		totalAssetTF.setText("---");
        	}
		}
	}
}
